package com.chapter.sample.answer.exporter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

@Slf4j
public class SpringELDemo {
    public static void main(String[] args) {
        //1、log日志占位{}替换
        log.info("log：「{}」","abcd");
        System.out.println();

        //2、String.format占位符替换
        String result = String.format("%s，java", "占位,study");
        System.out.println(result);
        System.out.println();

        //3、SpringELExpress表达式，#号后面的内容可以被具体值替换
        String var = "#userid";
        SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
        Expression expression = spelExpressionParser.parseExpression(var);

        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("userid","1234");

        String str = expression.getValue(context).toString();
        System.out.println(str);




        testParameterNameDiscoverer();
    }

    public static void testParameterNameDiscoverer() {
        ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
        Method method = ReflectionUtils.findMethod(TestController.class, "getParamName", String.class, Integer.class);
        String[] actualParams = discoverer.getParameterNames(method);
        for (String actualParam : actualParams) {
            System.out.println(actualParam);
        }
    }

    public class TestController {
        public String getParamName(String userName, Integer age) {
            System.out.println(userName);
            return userName;
        }
    }

}

